<?php
class Categories_m extends MY_Model{
	private $_table_lang;
	private $select;
	function __construct(){
		parent::__construct();
		$this->_table = 'polls_categories';
		$this->_table_lang = 'polls_categories_lang';
		$this->select = 'c.id,c.created,c.ordering,l.title';
	}
	
	public function get_all(){
		$this->_table = 'polls_categories c';
		$this->_table_lang = 'polls_categories_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang, 'c.id = l.id');
		$this->db->where('l.lang',$this->_lang);
		$this->db->order_by('c.ordering','desc');
		return parent::get_all();
	}
	
	public function insert($data){
		//add
		$add['created'] = strtotime(date('Y-m-d'));
		$add['status']	= 1;
		$id_insert = parent::insert($add);
		//add_lang
		$this->_table = 'polls_categories_lang';
		$add_lang['title'] = $data['title'];
		$add_lang['id'] = $id_insert;
		foreach($this->_lang_data as $l){
			$add_lang['lang'] = $l;
			parent::insert($add_lang);
		}
		return $id_insert;
	}
	
	public function update($id,$data){
		//update_lang // only
		$this->_table = 'polls_categories_lang';
		$update['title'] = $data['title'];
		$this->db->where('lang',$this->_lang);
		//$this->db->update($this->_table,$update);
		parent::update($id,$update);
	}
	
	public function delete($id){
		//xoa cat
		parent::delete($id);
		//xoa cat lang
		$this->_table = 'polls_categories_lang';
		$this->db->where('cat_id',$id);
		$this->db->delete($this->_table);
	}
	
	public function get($id){
		$this->_table = 'polls_categories c';
		$this->_table_lang = 'polls_categories_lang l';
		$this->db->select($this->select);
		$this->db->join($this->_table_lang, 'c.id = l.id');
		$this->db->where('c.id',$id);
		$this->db->where('l.lang',$this->_lang);
		return $this->db->get($this->_table)->row();
	}
	
	//
	public function ordering( $id, $model ){
		return parent::update($id,$model);
	}	
	
	function home( $id ){
		$this->db->update($this->_table,array('ordering'=>NULL));
		$data['ordering'] = 0;
		parent::update( $id , $data );
	}
	
	
}